<template>
	<!-- 选择基地，默认基地1 -->
	<view>
		<uni-data-select
			class="jidichoose"
		    v-model="baseSelectedValue"
		    :localdata="jidirange"
		    @change="jidichange"
			:clear="false"
		>
		</uni-data-select>
	</view>
	
	<!-- 大棚 -->
	<view>
		<view>
			<uni-section title="大棚列表" type="line">
			</uni-section>
			<uni-card v-for="(item,index) in greenhouseList" :key="item.id" padding="0" spacing="0" @click="goToIotDeviceRunData(item)">
				<template v-slot:cover>
					<view class="custom-cover-dapeng"  >
						<image class="cover-image"  :src="item.img" @click="goToIotDeviceRunData(item)">
						</image>
					</view>
				</template>
				<uni-list>
					<uni-list-item :title="item.name" showArrow></uni-list-item>
				</uni-list>
			</uni-card>
		</view>
	</view>
  
</template>

<script setup>
import {ref} from "vue"
import {getAllBaseinfoList} from "@/api/farm/farmBaseinfo.js"
import { onLoad } from '@dcloudio/uni-app'
import {getAllDaPenginfoList} from "@/api/farm/farmDapeng.js"
import { getDevCategory } from "@/api/iot/iotDeviceCategory"
import {API_BASE_URL} from "@/env_config.js"

const categoryCode=ref(null)
onLoad((options)=>{
	console.log("获取到的页面参数",options)
	categoryCode.value=options.categoryCode
})

//基地
const baseSelectedValue = ref(0);
const jidirange=ref([]);
// 大棚
const greenhouseList=ref([]);

/**
 * 获取所有基地信息
 */
function getAllBase(){
	getAllBaseinfoList({current:1,size:100})
	.then(res=>{
		console.log("获取所有基地信息",res)
		const jiDiList=res.data.data.records
		jiDiList.map(item=>{
			item.value=item.id;
			item.text=item.baseName
		})
		baseSelectedValue.value=jiDiList[2].value
		jidirange.value=jiDiList;
		getAllDaPengByBase()
		
	})
}
getAllBase()

/**
 * 获取所有大棚信息
 */
function getAllDaPengByBase(){
	getAllDaPenginfoList({current:1,size:100,baseinfoId:baseSelectedValue.value})
	.then(res=>{
		console.log("获取所有大棚信息",res)
		const dapengList=res.data.data.records
		dapengList.map(item=>{
			item.id = item.id;
			item.name = item.houseName;
			item.area = item.houseArea;
			const image = item.houseImg ? `${API_BASE_URL}${item.houseImg}` : '';
			item.img =image
			// item.img ="@/static/images/test.png"
		})
		greenhouseList.value=dapengList;
	})
}


function jidichange(e) {
    console.log("e:", e);
	getAllDaPengByBase()
}


/**
 * 路由跳转到 根据物联设备的不同 跳转到不太的设备数据界面
 */
function goToIotDeviceRunData(greenhouse){
		
	if(categoryCode.value==="kgNpk"){
		getDevCategory({categoryCode:categoryCode.value})
		.then(res=>{
			const deviceCategoryId=res.data.data.records[0].id
			uni.navigateTo({
				url:"/pages/home/dataCollection/soilMonitoring/soilMonitoring?deviceCategoryId="+deviceCategoryId+"&greenhouseId="+greenhouse.id
			})
		})
		
	}
	
	if(categoryCode.value==="kgBotCam"){
		getDevCategory({categoryCode:categoryCode.value})
		.then(res=>{
			const deviceCategoryId=res.data.data.records[0].id
			uni.navigateTo({
				url:"/pages/home/dataCollection/pestidentification/pestidentification?deviceCategoryId="+deviceCategoryId+"&greenhouseId="+greenhouse.id
			})
		})
		
	}
	
	if(categoryCode.value==="testShengZhang"){
		uni.navigateTo({
			url:"/pages/home/dataCollection/growReproduce/growReproduce"
		})
	}
	
	
}


</script>

<style lang="scss">
:deep(.jidichoose){
	width: 50vw;
	.uni-stat__select{
		width: 50vw;
		margin-left: 30rpx;
		margin-top: 10rpx;
	}
	.custom-dropdown-icon {
	    display: inline-block;
	    margin-left: 10px;
	    cursor: pointer;
	}
}

.custom-cover-dapeng {
	flex: 1;
	flex-direction: row;
	position: relative;
	height: 250rpx;
	image{
		width: 100%;
	}
}

</style>
